/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.produto;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.produto.CategoriaInterface;
import br.gov.cemaden.library.model.produto.Categoria;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class CategoriaDao extends GenericDao implements CategoriaInterface {

    private Session session;

    public CategoriaDao(Session session) {
        this.session = session;
    }

    @Override
    public List<Categoria> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Categoria> categoria = null;
        try {
            Query query = session.getNamedQuery("Categoria.findAll");
            categoria = (List<Categoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CategoriaDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return categoria;
    }

    @Override
    public Categoria findByIdCategoria(Long idCategoria) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Categoria categoria = null;
        try {
            Query query = session.getNamedQuery("Categoria.findByIdCategoria").setParameter("idCategoria", idCategoria);
            categoria = (Categoria) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CategoriaDao.findByIdCategoria: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return categoria;
    }

    @Override
    public List<Categoria> findByDescricao(String descricao) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Categoria> categoria = null;
        try {
            Query query = session.getNamedQuery("Categoria.findByDescricao").setParameter("descricao", descricao);
            categoria = (List<Categoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CategoriaDao.findByDescricao: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return categoria;
    }

    @Override
    public List<Categoria> findByPrefixo(String prefixo) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Categoria> categoria = null;
        try {
            Query query = session.getNamedQuery("Categoria.findByPrefixo").setParameter("prefixo", prefixo);
            categoria = (List<Categoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no CategoriaDao.findByPrefixo: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return categoria;
    }

    @Override
    public void insert(Categoria categoria) {
        try {
            saveOrUpdate(this.session, categoria);
        } catch (Exception ex) {
            Logger.getLogger(CategoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Categoria categoria) {
        try {
            saveOrUpdate(this.session, categoria);
        } catch (Exception ex) {
            Logger.getLogger(CategoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Categoria categoria) {
        try {
            remove(this.session, categoria);
        } catch (Exception ex) {
            Logger.getLogger(CategoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
